package com.lhy.es.controller;

import com.lhy.es.entity.Document;
import com.lhy.es.mapper.DocumentMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.core.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return documentMapper.createIndex();
    }

    @PostMapping("/insert")
    public Integer insert(@RequestBody Document document) {
        // 2.初始化-> 新增数据
        return documentMapper.insert(document);
    }

    @PostMapping("/update")
    public Integer update(@RequestBody Document document) {
        return documentMapper.updateById(document);
    }

    @PostMapping("/deleteById")
    public Integer deleteById(@RequestBody Document document) {
        return documentMapper.deleteById(document.getId());
    }

    @PostMapping("/deleteByTitle")
    public Integer deleteByTitle(@RequestBody Document document) {
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, document.getTitle());
        return documentMapper.delete(wrapper);
    }

    @GetMapping("/search")
    public List<Document> search(@RequestParam("title") String title) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.like(Document::getTitle, title);
        return documentMapper.selectList(wrapper);
    }
}